using System;
using System.Windows.Forms;
using StockholmsStad.CreateTestData.WinService;
using StockholmsStad.CreateTestData.TicketService;
using System.Collections.Generic;

namespace StockholmsStad.CreateTestData
{
    public partial class Form1 : Form
    {
        private readonly TicketService.User userT = new TicketService.User { Username = "parvis@input.nu", Password = "mario66lemieux", Role = TicketService.UserRole.SuperUser };
        private readonly WinService.User userW = new WinService.User { Username = "parvis@input.nu", Password = "mario66lemieux", Role = WinService.UserRole.SuperUser };

        #region lists
        private readonly List<string> maleNames = new List<string>()
                                                        {
                                                          "Pelle",
                                                          "Nils",
                                                          "Anders",
                                                          "Måns",
                                                          "Fredrik",
                                                          "Henrik",
                                                          "Jonas",
                                                          "David",
                                                          "Peter",
                                                          "Kalle",
                                                          "Oscar",
                                                          "Erik",
                                                          "Joakim",
                                                          "Jens",
                                                          "Kristoffer",
                                                          "Jörgen",
                                                          "Janne",
                                                          "Håkan",
                                                          "Thomas",
                                                          "Magnus",
                                                          "Mikael",
                                                          "Hasse",
                                                          "Göran",
                                                          "Leif",
                                                          "Urban",
                                                          "Roland",
                                                          "Bengt",
                                                          "Isak",
                                                          "Patrik",
                                                          "Zlatan",
                                                          "Nicklas",
                                                          "Simon",
                                                          "Sven",
                                                        };
        private readonly List<string> femaleNames = new List<string>()
                                                        {
                                                          "Petra",
                                                          "Nina",
                                                          "Anna",
                                                          "Maria",
                                                          "Åsa",
                                                          "Birgitta",
                                                          "Carina",
                                                          "Johanna",
                                                          "Susanna",
                                                          "Kristina",
                                                          "Linnea",
                                                          "Elsa",
                                                          "Hulda",
                                                          "Frida",
                                                          "Wilma",
                                                          "Tintin",
                                                          "Amanda",
                                                          "Moa",
                                                          "Molly",
                                                          "Karin",
                                                          "Sara",
                                                          "Malin",
                                                          "Caroline",
                                                          "Alice",
                                                          "Emma",
                                                          "Felicia",
                                                          "Melina",
                                                          "Jenny",
                                                          "Tina",
                                                        };
        private readonly List<string> lastNames = new List<string>()
                                                        {
                                                          "Pettersson",
                                                          "Nilsson",
                                                          "Andersson",
                                                          "Månsson",
                                                          "Fredriksson",
                                                          "Henriksson",
                                                          "Jonasson",
                                                          "Davidsson",
                                                          "Gustavsson",
                                                          "Kristensson",
                                                          "Oscarsson",
                                                          "Eriksson",
                                                          "Lundberg",
                                                          "Hansson",
                                                          "Larsson",
                                                          "Torsson",
                                                          "Janson",
                                                          "Håkansson",
                                                          "Thomasson",
                                                          "Magnusson",
                                                        };
        private readonly List<string> streetAddresses = new List<string>()
                                                        {
                                                          "Kungsg",
                                                          "Vitnäsv",
                                                          "Vasag",
                                                          "Rondov",
                                                          "Paradisv",
                                                          "Torsg",
                                                          "Gubbgränd",
                                                          "Döbelnsg",
                                                          "Eriksg",
                                                          "Utanbyv",
                                                          "Irisv",
                                                        };
        private readonly List<string> cities = new List<string>()
                                                        {
                                                          "Huddinge",
                                                          "Stockholm",
                                                          "Örebro",
                                                          "Farsta",
                                                          "Solna",
                                                          "Täby",
                                                          "Bromma",
                                                          "Danderyd",
                                                          "Vallentuna",
                                                          "Lidingö",
                                                          "Nacka",
                                                        };
        #endregion

        public Form1()
        {
            //Kom ihåg att ställa om i app.config vilken server man ska köra mot!!!
            InitializeComponent();

            //TODO 
            //Hämta max ticketId + 1 för att använda som start värde för i
            //Slumpa fram partnerskap
            //Slumpa fram icke svensk
            //Slumpa fram skyddad person


        }

        private void button1_Click(object sender, EventArgs e)
        {
            var service = new WinServiceClient();

            int count = string.IsNullOrEmpty(tbNumMinisters.Text) ? 10 : Int32.Parse(tbNumMinisters.Text);

            for (int i=0; i<count;i++)
            {
                var add = CreateAddressW(i);
                var minister = new WinService.Minister()
                                   {
                                       Address = add,
                                       AssignmentStartDate = new DateTime(2009, 1, 1),
                                       AssignmentEndDate = new DateTime(2011, 1, 1),
                                       MinisterCode = "M" + PadZeros(i, 5),
                                       BirthdayNumber = "199001010009",
                                       CellPhoneNumber = "0123-456789",
                                       Email = "minister" + i + "@input.nu",
                                       FirstNames = "Förrättare-" + PadZeros(i, 2),
                                       LastName = GetLastName(),
                                       PhoneNumber = "08-888555",
                                       Languages = new[]{"Svenska"},
                                       SpecialAssignment = false,
                                   };

                //var b = minister.Address.PostCode;
                var result = service.AddMinister(userW, minister);
                //var min = service.GetMinister(userW, result);

            }
        }

        private string GetStreetAddress(int number)
        {
            var c = streetAddresses.Count - 1;
            var r = new Random();
            var s = streetAddresses[r.Next(c)] + " " + PadZeros(number, 2);
            return s;
        }

        private string GetCity()
        {
            var c = cities.Count - 1;
            var r = new Random();
            var s = cities[r.Next(c)];
            return s;
        }

        private string GetMaleFirstName()
        {
            var c = maleNames.Count - 1;
            var r = new Random();
            var s = maleNames[r.Next(c)];
            return s;
        }

        private string GetFemaleFirstName()
        {
            var c = femaleNames.Count - 1;
            var r = new Random();
            var s = femaleNames[r.Next(c)];
            return s;
        }

        private string GetLastName()
        {
            var c = lastNames.Count - 1;
            var r = new Random();
            var s = lastNames[r.Next(c)];
            return s;
        }

        private WinService.Address CreateAddressW(int number)
        {
            var ac = streetAddresses.Count;
            var cc = cities.Count;
            var r = new Random();
            var add = new WinService.Address()
                          {
                              StreetAddress = GetStreetAddress(number),
                              PostCode = "" + PadZeros(number, 5),
                              City = GetCity(),
                              Country = "Sverige",

                          };
            return add;
        }

        private TicketService.Address CreateAddressT(int number)
        {
            var add = new TicketService.Address()
            {
                StreetAddress = GetStreetAddress(number),
                PostCode = "" + PadZeros(number, 5),
                City = GetCity(),
                Country = "Sverige",

            };
            return add;
        }

        private TicketService.Person GetFemale(int number)
        {
            var female = new TicketService.Person()
            {
                CellPhoneNumber = "070-721" + PadZeros(number,3),
                Email = "",
                FirstNames = GetFemaleFirstName(),
                LastName = GetLastName(),
                Nationality = "Svensk",
                PersonalNumber = "195401090000",
                PhoneNumber = "08-850" + PadZeros(number, 3),
                ProtectedIdentity = false,
                Sex = Sex.Female,
                SwedishNationality = true,
            };
            return female;
        }

        private TicketService.Person GetMale(int number)
        {
            var male = new TicketService.Person()
            {
                CellPhoneNumber = "070-341" + PadZeros(number, 3),
                Email = "",
                FirstNames = GetMaleFirstName(),
                LastName = GetLastName(),
                Nationality = "Svensk",
                PersonalNumber = "195401090000",
                PhoneNumber = "08-487" + PadZeros(number, 3),
                ProtectedIdentity = false,
                Sex = Sex.Male,
                SwedishNationality = true,
            };
            return male;
        }

        private static string PadZeros(int number, int size)
        {
            var sz = "";
            for (int i=0; i< size; i++)
            {
                sz += "0";
            }
            return number.ToString(sz);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int count = string.IsNullOrEmpty(tbNumCouples.Text) ? 10 : Int32.Parse(tbNumCouples.Text);

            progressBar1.Minimum = 0;
            progressBar1.Maximum = count;

            var start = DateTime.Now;

            for (int i = 0; i < count; i++)
            {

                progressBar1.Value = i;
                Application.DoEvents();

                var rand = new Random();

                var add = i % 5 != 0 ? CreateAddressT(i) : null;
                var p1 = GetFemale(i);
                p1.Address = add;
                var p2 = GetMale(i);

                var con = new TicketService.ContactPerson()
                {
                    FirstNames = i % 2 != 0 ? GetFemaleFirstName() : GetMaleFirstName(),
                    LastName = GetLastName(),
                    Email = i % 2 != 0 ? "parvis." + rand.Next(1, 1000) + "." + PadZeros(i,6) + "@input.nu" : null,
                    Telephone = "08-7" + PadZeros(i, 6),
                };
                var couple = new TicketService.Couple()
                {
                    ContactPerson = con,
                    Person1 = p1,
                    Person2 = p2,
                };

                var service = new TicketServiceClient();

                var ticketId = service.AddTicket(userT, couple, null);

                var win = new WinServiceClient();

                var ministers = win.GetMinisters(userW);

                var min1 = ministers[3];
                var min2 = ministers[2];
                var min3 = ministers[0];

                var sdt = DateTime.Now;
                if(count < 100)
                {
                    sdt = sdt.AddDays(i);
                }
                else if (count < 1000)
                {
                    sdt = sdt.AddMonths(2).AddHours(i);
                }
                else if (count < 10000)
                {
                    sdt = sdt.AddMonths(2).AddMinutes(i);
                }
                
                var mtb = new MinisterTimeBlock()
                             {
                                 StartDateTime = sdt,
                                 Minister = i % 3 != 0 ? min1 : min3,
                                 TempMinister = i % 3 != 0 ? min2 : min1,
                                 TimeBlockStatus = TimeBlockStatus.Bookable,
                             };

                var tbList = new List<MinisterTimeBlock> {mtb};

                win.UpdateTimeBlocks(userW, tbList.ToArray());

                var freeTbs = win.GetFreeTimeBlocks(userW);

                var tb = freeTbs[0];

                //var tb = new TimeBlock()
                //{
                //    StartDateTime = freeTbs[0].StartDateTime,
                //};

                var ct = i % 8 != 0 ? CeremonyType.Ordinary : CeremonyType.Individual;

                var mincode = i%5 != 0 ? min1.MinisterCode : min3.MinisterCode;

                var pfdt1 = tb.StartDateTime.AddDays(14);
                var pfdt2 = tb.StartDateTime.AddDays(21);


                var booking = new Booking()
                                  {
                                      CeremonyType = ct,
                                      DateTime = tb,
                                      MinisterCode = ct == CeremonyType.Individual ? mincode : "",
                                      PreferredDate1 = i % 5 != 0 ? pfdt1 : (DateTime?) null,
                                      PreferredDate2 = i % 9 != 0 ? pfdt2 : (DateTime?) null,
                }
                ;
                //var cp = service.GetCouple(userT, result);

                win.SaveBooking(userW, ticketId, booking);

                service.Close();
                
                win.Close();

            }

            var time = DateTime.Now.Subtract(start);

            MessageBox.Show("Klar! " + time);

        }

    }
}
